Conditions | 4 |
Paths | 170 |
Total Lines | 152 |
Code Lines | 104 |
Lines | 42 |
Ratio | 27.63 % |
Changes | 4 | ||
Bugs | 1 | Features | 0 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
1 | /** |
||
35 | initDropzone() { |
||
36 | if (typeof Dropzone != 'undefined') { |
||
37 | Dropzone.autoDiscover = false; |
||
38 | |||
39 | // Cards version |
||
40 | if($('#dropzone-cards').length) { |
||
41 | let dropzoneCards = $('#dropzone-cards'); |
||
42 | let dropzoneCardsActionUrl = dropzoneCards.data('action-url'); |
||
43 | |||
44 | let dropzoneCardsFilePreview = dropzoneCards.find('#dropzone-cards-template'); |
||
45 | dropzoneCardsFilePreview.removeAttr('id'); |
||
46 | |||
47 | let dropzoneCardsFilePreviewTemplate = dropzoneCardsFilePreview.parentNode.innerHTML; |
||
48 | dropzoneCardsFilePreviewTemplate.parentNode.removeChild(dropzoneCardsFilePreview); |
||
49 | |||
50 | let dropzoneCards = $('#dropzone-cards-form').dropzone({ |
||
|
|||
51 | url: dropzoneCardsActionUrl, |
||
52 | autoProcessQueue: true, |
||
53 | thumbnailWidth: null, |
||
54 | thumbnailHeight: null, |
||
55 | previewTemplate: dropzoneCardsFilePreviewTemplate |
||
56 | }); |
||
57 | |||
58 | dropzoneCards.on("addedfile", function (file) { |
||
59 | var fileId = 'media' + document.querySelectorAll('.media-list-item').length; |
||
60 | file.previewElement.getElementsByTagName('input')[0].setAttribute('id', fileId); |
||
61 | file.previewElement.getElementsByTagName('label')[0].setAttribute('for', fileId); |
||
62 | |||
63 | var imagesFileTypes = ['image/png', 'image/jpg', 'image/jpeg', 'image/gif']; |
||
64 | View Code Duplication | if (imagesFileTypes.indexOf(file.type) != -1) { |
|
65 | file.previewElement.querySelector('.media-item-file-details').style.display = 'none'; |
||
66 | } else if (file.type === 'application/pdf') { |
||
67 | file.previewElement.querySelector('.media-item-file-details').style.display = 'block'; |
||
68 | file.previewElement.querySelector('.media-item-icon').innerHTML = '<i class="fas fa-file-pdf"></i>'; |
||
69 | } else if (file.type === 'application/doc' | 'application/docx') { |
||
70 | file.previewElement.querySelector('.media-item-file-details').style.display = 'block'; |
||
71 | file.previewElement.querySelector('.media-item-icon').innerHTML = '<i class="fas fa-file-word"></i>'; |
||
72 | } else if (file.type === 'application/ppt' | 'application/pptx') { |
||
73 | file.previewElement.querySelector('.media-item-file-details').style.display = 'block'; |
||
74 | file.previewElement.querySelector('.media-item-icon').innerHTML = '<i class="fas fa-file-powerpoint"></i>'; |
||
75 | } else if (file.type === 'video/mp4' | 'video/webm' | 'video/mkv') { |
||
76 | file.previewElement.querySelector('.media-item-file-details').style.display = 'block'; |
||
77 | file.previewElement.querySelector('.media-item-icon').innerHTML = '<i class="fas fa-file-video"></i>'; |
||
78 | } else if (file.type === 'audio/mpeg') { |
||
79 | file.previewElement.querySelector('.media-item-file-details').style.display = 'block'; |
||
80 | file.previewElement.querySelector('.media-item-icon').innerHTML = '<i class="fas fa-file-audio"></i>'; |
||
81 | } else { |
||
82 | file.previewElement.querySelector('.media-item-file-details').style.display = 'block'; |
||
83 | file.previewElement.querySelector('.media-item-icon').innerHTML = '<i class="fas fa-file"></i>'; |
||
84 | } |
||
85 | }); |
||
86 | |||
87 | dropzoneCards.on("success", function (file, resp) { |
||
88 | file.previewElement.querySelector(".media-list-item").classList.remove('uploading'); |
||
89 | file.previewElement.querySelector(".upload-progress").style.display = 'none'; |
||
90 | file.previewElement.querySelector(".media-item-file-extension").innerHTML = file.type; |
||
91 | }); |
||
92 | |||
93 | dropzoneCards.on("error", function (file) { |
||
94 | file.previewElement.querySelector(".media-list-item").classList.remove('uploading'); |
||
95 | file.previewElement.querySelector(".upload-progress").style.display = 'none'; |
||
96 | file.previewElement.querySelector(".media-item-file-extension").innerHTML = file.type; |
||
97 | }); |
||
98 | } |
||
99 | |||
100 | // Table version |
||
101 | if($('#dropzone-table').length) { |
||
102 | let dropzoneTable = $('#dropzone-table'); |
||
103 | let dropzoneTableActionUrl = dropzoneTable.data('action-url'); |
||
104 | |||
105 | let dropzoneTableFilePreview = dropzoneTable.find('#dropzone-table-template'); |
||
106 | dropzoneTableFilePreview.removeAttr('id'); |
||
107 | |||
108 | let dropzoneTableFilePreviewTemplate = dropzoneTableFilePreview.parentNode.innerHTML; |
||
109 | dropzoneTableFilePreviewTemplate.parentNode.removeChild(dropzoneTableFilePreview); |
||
110 | |||
111 | let dropzoneTable = $('#dropzone-table-form').dropzone({ |
||
112 | url: dropzoneTableActionUrl, |
||
113 | autoProcessQueue: false, |
||
114 | thumbnailWidth: null, |
||
115 | thumbnailHeight: null, |
||
116 | previewTemplate: dropzoneTableFilePreviewTemplate, // Define the container to display the previews |
||
117 | previewsContainer: '#dropzone-table', |
||
118 | clickable: "#dropzone-add-file", // Define the element that should be used as click trigger to select files. |
||
119 | }); |
||
120 | |||
121 | dropzoneTable.on("addedfile", function (file) { |
||
122 | |||
123 | var imagesFileTypes = ['image/png', 'image/jpg', 'image/jpeg', 'image/gif']; |
||
124 | View Code Duplication | if (imagesFileTypes.indexOf(file.type) != -1) { |
|
125 | file.previewElement.querySelector('.media-item-file-details').style.display = 'none'; |
||
126 | } else if (file.type === 'application/pdf') { |
||
127 | file.previewElement.querySelector('.media-item-file-details').style.display = 'block'; |
||
128 | file.previewElement.querySelector('.media-item-icon').innerHTML = '<i class="fas fa-file-pdf"></i>'; |
||
129 | } else if (file.type === 'application/doc' | 'application/docx') { |
||
130 | file.previewElement.querySelector('.media-item-file-details').style.display = 'block'; |
||
131 | file.previewElement.querySelector('.media-item-icon').innerHTML = '<i class="fas fa-file-word"></i>'; |
||
132 | } else if (file.type === 'application/ppt' | 'application/pptx') { |
||
133 | file.previewElement.querySelector('.media-item-file-details').style.display = 'block'; |
||
134 | file.previewElement.querySelector('.media-item-icon').innerHTML = '<i class="fas fa-file-powerpoint"></i>'; |
||
135 | } else if (file.type === 'video/mp4' | 'video/webm' | 'video/mkv') { |
||
136 | file.previewElement.querySelector('.media-item-file-details').style.display = 'block'; |
||
137 | file.previewElement.querySelector('.media-item-icon').innerHTML = '<i class="fas fa-file-video"></i>'; |
||
138 | } else if (file.type === 'audio/mpeg') { |
||
139 | file.previewElement.querySelector('.media-item-file-details').style.display = 'block'; |
||
140 | file.previewElement.querySelector('.media-item-icon').innerHTML = '<i class="fas fa-file-audio"></i>'; |
||
141 | } else { |
||
142 | file.previewElement.querySelector('.media-item-file-details').style.display = 'block'; |
||
143 | file.previewElement.querySelector('.media-item-icon').innerHTML = '<i class="fas fa-file"></i>'; |
||
144 | } |
||
145 | // Hookup the start button |
||
146 | file.previewElement.querySelector(".start").onclick = function() { fileDropzone.enqueueFile(file); }; |
||
147 | }); |
||
148 | |||
149 | dropzoneTable.on("totaluploadprogress", function(progress) { |
||
150 | document.querySelector("#dropzone-table-total-progress .progress-bar").style.width = progress + "%"; |
||
151 | }); |
||
152 | |||
153 | dropzoneTable.on("sending", function(file) { |
||
154 | // Show the total progress bar when upload starts |
||
155 | document.querySelector("#dropzone-table-total-progress").style.opacity = "1"; |
||
156 | // And disable the start button |
||
157 | file.previewElement.querySelector(".start").setAttribute("disabled", "disabled"); |
||
158 | }); |
||
159 | |||
160 | dropzoneTable.on("success", function (file, resp) { |
||
161 | file.previewElement.querySelector(".upload-progress").style.display = 'none'; |
||
162 | file.previewElement.querySelector(".media-item-file-extension").innerHTML = file.type; |
||
163 | }); |
||
164 | |||
165 | dropzoneTable.on("error", function (file) { |
||
166 | file.previewElement.querySelector(".upload-progress").style.display = 'none'; |
||
167 | file.previewElement.querySelector(".media-item-file-extension").innerHTML = file.type; |
||
168 | }); |
||
169 | |||
170 | fileDropzone.on("queuecomplete", function(progress) { |
||
171 | document.querySelector("#dropzone-table-total-progress").style.opacity = "0"; |
||
172 | }); |
||
173 | |||
174 | // Setup the buttons for all transfers |
||
175 | // The "add files" button doesn't need to be setup because the config |
||
176 | // `clickable` has already been specified. |
||
177 | document.querySelector("#dropzone-table-actions .start").onclick = function() { |
||
178 | dropzoneTable.enqueueFiles(fileDropzone.getFilesWithStatus(Dropzone.ADDED)); |
||
179 | }; |
||
180 | |||
181 | document.querySelector("#dropzone-table-actions .cancel").onclick = function() { |
||
182 | dropzoneTable.removeAllFiles(true); |
||
183 | }; |
||
184 | } |
||
185 | } |
||
186 | } |
||
187 | |||
191 | } |
This check looks for variables that are declared in multiple lines. There may be several reasons for this.
In the simplest case the variable name was reused by mistake. This may lead to very hard to locate bugs.
If you want to reuse a variable for another purpose, consider declaring it at or near the top of your function and just assigning to it subsequently so it is always declared.